package com.todo.auth.common.convert;

import com.todo.admin.common.base.SystemLoginUser;
import com.todo.auth.entity.UserAccountDetail;
import com.todo.common.common.entity.CurrentStaff;
import com.todo.common.common.entity.CurrentUser;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.user.api.response.UserAccountResponse;

/**
 * @author azhebuxing
 * @date 2025/2/21 15:36
 * @description
 */
public class UserAuthConvert {

    public static UserAccountDetail.UserAccount convertToUserAccount(UserAccountResponse userAccountResponse) {
        if (userAccountResponse == null) {
            return null;
        }
        UserAccountDetail.UserAccount userAccount = new UserAccountDetail.UserAccount();
        userAccount.setUserId(userAccountResponse.getUserId());
        userAccount.setUserName(userAccountResponse.getUserName());
        userAccount.setPassword(userAccountResponse.getPassword());
        userAccount.setSource(userAccountResponse.getSource().getType());
        userAccount.setNickName(userAccountResponse.getNickName());
        userAccount.setGender(userAccountResponse.getGender());
        userAccount.setHeadImg(userAccountResponse.getHeadImg());
        userAccount.setIsReal(userAccountResponse.getIsReal());
        return userAccount;
    }

    public static CurrentUser buildCurrentUser(UserAccountDetail userAccountDetail) {
        if (userAccountDetail == null || userAccountDetail.getUserAccount() == null) {
            throw new BizException(BizErrorCodeEnum.PLACE_LOGIN_AGAIN, "当前登录信息异常");
        }
        CurrentUser currentUser = new CurrentUser();
        currentUser.setUserId(userAccountDetail.getUserAccount().getUserId());
        currentUser.setUserName(userAccountDetail.getUserAccount().getUserName());
        currentUser.setSource(userAccountDetail.getUserAccount().getSource());
        currentUser.setAvatar(userAccountDetail.getUserAccount().getHeadImg());
        if (userAccountDetail.getLoginInfo() != null) {
            currentUser.setIpAddress(userAccountDetail.getLoginInfo().getIpAddress());
            currentUser.setLocation(userAccountDetail.getLoginInfo().getLocation());
            currentUser.setBrowser(userAccountDetail.getLoginInfo().getBrowser());
            currentUser.setOperationSystem(userAccountDetail.getLoginInfo().getOperationSystem());
            currentUser.setLoginTime(userAccountDetail.getLoginInfo().getLoginTime());
        }
        return currentUser;
    }

    public static CurrentStaff buildCurrentStaff(SystemLoginUser loginUser) {
        if (loginUser == null) {
            throw new BizException(BizErrorCodeEnum.PLACE_LOGIN_AGAIN, "当前登录信息异常");
        }
        CurrentStaff staff = new CurrentStaff();
        staff.setUserId(loginUser.getUserId());
        staff.setUsername(loginUser.getUsername());
        staff.setDeptId(loginUser.getDeptId());
        staff.setRoleId(loginUser.getRoleId());
        staff.setIsAdmin(loginUser.isAdmin());
        if (loginUser.getLoginInfo() != null) {
            staff.setIpAddress(loginUser.getLoginInfo().getIpAddress());
            staff.setLocation(loginUser.getLoginInfo().getLocation());
            staff.setBrowser(loginUser.getLoginInfo().getBrowser());
            staff.setOperationSystem(loginUser.getLoginInfo().getOperationSystem());
            staff.setLoginTime(loginUser.getLoginInfo().getLoginTime());
        }
        return staff;
    }
}
